{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "342cf646",
   "metadata": {},
   "source": [
    "-------\n",
    "\n",
    "* 本周主要内容：语音识别\n",
    "* 22春_API_人工智能与机器学习_week12\n",
    "*  电子讲义设计者：许智超\n",
    "<br/>\n",
    "<br/>\n",
    "\n",
    "-------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80d2baa7",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "## 优化ASR(自动语音识别)机器人\n",
    "\n",
    "## 连接图灵机器人\n",
    "\n",
    "## 语音合成"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ddd4bd2",
   "metadata": {},
   "source": [
    "# 优化ASR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "50aacbe6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "77e6591e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# zhichao : asr\n",
    "API_KEY = 'smxkOHWjqLVljEmIry5vuSYI'\n",
    "SECRET_KEY = 'ky3FZeSSDurxLyLLqZ2kPaEnts9NH1W1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a11ca8da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.b89c4ead04387e202522cab126d89fdf.2592000.1655279745.282335-19331335'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xu_token = asr.fetch_token(API_KEY,SECRET_KEY)\n",
    "xu_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c09431ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7098239816565607830\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"你好。\"],\"sn\":\"790874249121652687745\"}\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr.asr(xu_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "919f9806",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['你好。']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr_result = eval(asr.asr(xu_token))[\"result\"]\n",
    "asr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "656236c4",
   "metadata": {},
   "source": [
    "# 图灵机器人"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ce0b53f",
   "metadata": {},
   "source": [
    "## 中英互译\n",
    "\n",
    "* 能力介绍\n",
    "> 中英互译是一项在线翻译服务，提供中文和英语的互译服务。可以通过使用中英互译功能为用户提供实时优质的翻译服务，提升产品体验。您只需要通过开启此功能，传入待翻译的内容，并指定要翻译的源语言和目标语言种类，就可以得到相应的翻译结果。\n",
    "\n",
    "* 使用示例\n",
    "> “苹果用英语怎么说”“苹果英语怎么说”“英语说苹果”\n",
    "\n",
    "* url\n",
    "> http://www.tuling123.com/openapi/api\n",
    "\n",
    "* 请求方法\n",
    "> HTTP  POST\n",
    "\n",
    "* 请求参数\n",
    "<img src=\"req_params.jpg\" width=70%></img>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "ffa28ebd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "11eabc36",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_key = '你的图灵机器人key'\n",
    "TL_url = \"http://openapi.turingapi.com/openapi/api/v2\"\n",
    "\n",
    "payload={\n",
    "    \"reqType\":0,\n",
    "    \"perception\": {\n",
    "        \"inputText\": {\n",
    "            \"text\": \"苹果英语怎么说\"\n",
    "        },\n",
    "    },\n",
    "    \"userInfo\": {\n",
    "        \"apiKey\": TL_key,\n",
    "        \"userId\": \"1234\"\n",
    "    }\n",
    "}\n",
    "\n",
    "res = requests.post(TL_url,data=json.dumps(payload))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "287eca0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "3e58b5e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'apple'"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res.json()['results'][0]['values']['text']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "2a81ef9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装函数\n",
    "def TuLing(TL_key,text):   \n",
    "    TL_url = \"http://openapi.turingapi.com/openapi/api/v2\"\n",
    "    payload={\n",
    "        \"reqType\":0,\n",
    "        \"perception\": {\n",
    "            \"inputText\": {\n",
    "                \"text\": text\n",
    "            },\n",
    "        },\n",
    "        \"userInfo\": {\n",
    "            \"apiKey\": TL_key,\n",
    "            \"userId\": \"1234\"\n",
    "        }\n",
    "    }\n",
    "\n",
    "    res = requests.post(TL_url,data=json.dumps(payload))\n",
    "    return res.json()['results'][0]['values']['text']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf8abd0d",
   "metadata": {},
   "source": [
    "## 实践：自动回复机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "712f180c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'黑人我在看球赛，两岁的女儿也在旁边凑热闹。忽然她指着电视说，“爸爸，这几个叔叔是不是弄丢了户口本？”我不解地问：“为什么？“妈妈不让我玩户口本，说弄丢了我们都会变成黑人！”'"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import speech_recognition\n",
    "\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    \n",
    "asr.asr(xu_token) \n",
    "asr_result = eval(asr.asr(xu_token))[\"result\"]\n",
    "TuLing(TL_key,asr_result[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "410f1438",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "\n",
    "from pydub.playback import play\n",
    "\n",
    "song = AudioSegment.from_wav('1.wav')\n",
    "\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a36a6a4",
   "metadata": {},
   "source": [
    "# 语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "eea54509",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fetch token begin\n",
      "{\"refresh_token\":\"25.c9a9507dee0150bf1ab73ba0ad839b6d.315360000.1968057035.282335-19331335\",\"expires_in\":2592000,\"session_key\":\"9mzdWWgIBtYjZZk3jnlbwHefMb4BEkoPfjkngK4JP2zycqn7BDHFne90e\\/IVMqEpDb8aBisl+zzfFOvUJLHhODD3nh43XA==\",\"access_token\":\"24.b2d376e00733bdf5523edd1ffbca9663.2592000.1655289035.282335-19331335\",\"scope\":\"brain_asr_async brain_speech_realtime audio_voice_assistant_get brain_enhanced_asr audio_tts_post public brain_all_scope picchain_test_picchain_api_scope wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"023929b8bbc3d4bbea75c742c8d6ddb0\"}\n",
      "\n",
      "{'refresh_token': '25.c9a9507dee0150bf1ab73ba0ad839b6d.315360000.1968057035.282335-19331335', 'expires_in': 2592000, 'session_key': '9mzdWWgIBtYjZZk3jnlbwHefMb4BEkoPfjkngK4JP2zycqn7BDHFne90e/IVMqEpDb8aBisl+zzfFOvUJLHhODD3nh43XA==', 'access_token': '24.b2d376e00733bdf5523edd1ffbca9663.2592000.1655289035.282335-19331335', 'scope': 'brain_asr_async brain_speech_realtime audio_voice_assistant_get brain_enhanced_asr audio_tts_post public brain_all_scope picchain_test_picchain_api_scope wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '023929b8bbc3d4bbea75c742c8d6ddb0'}\n",
      "SUCCESS WITH TOKEN: 24.b2d376e00733bdf5523edd1ffbca9663.2592000.1655289035.282335-19331335 ; EXPIRES IN SECONDS: 2592000\n",
      "%E6%AC%A2%E8%BF%8E%E4%BD%BF%E7%94%A8%E7%99%BE%E5%BA%A6%E8%AF%AD%E9%9F%B3%E5%90%88%E6%88%90%E3%80%82\n",
      "test on Web Browserhttp://tsn.baidu.com/text2audio?tok=24.b2d376e00733bdf5523edd1ffbca9663.2592000.1655289035.282335-19331335&tex=%25E6%25AC%25A2%25E8%25BF%258E%25E4%25BD%25BF%25E7%2594%25A8%25E7%2599%25BE%25E5%25BA%25A6%25E8%25AF%25AD%25E9%259F%25B3%25E5%2590%2588%25E6%2588%2590%25E3%2580%2582&per=4&spd=5&pit=5&vol=5&aue=3&cuid=123456PYTHON&lan=zh&ctp=1\n",
      "tts api  error:{\"err_detail\":\"16: Open api characters limit reached\",\"err_msg\":\"16: Open api characters limit reached\",\"err_no\":502,\"err_subcode\":16,\"tts_logid\":4149813859}\n",
      "\n",
      "result saved as :error.txt\n"
     ]
    }
   ],
   "source": [
    "# coding=utf-8\n",
    "import sys\n",
    "import json\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "if IS_PY3:\n",
    "    from urllib.request import urlopen\n",
    "    from urllib.request import Request\n",
    "    from urllib.error import URLError\n",
    "    from urllib.parse import urlencode\n",
    "    from urllib.parse import quote_plus\n",
    "else:\n",
    "    import urllib2\n",
    "    from urllib import quote_plus\n",
    "    from urllib2 import urlopen\n",
    "    from urllib2 import Request\n",
    "    from urllib2 import URLError\n",
    "    from urllib import urlencode\n",
    "\n",
    "# API_KEY = '4E1BG9lTnlSeIf1NQFlrSq6h'\n",
    "# SECRET_KEY = '544ca4657ba8002e3dea3ac2f5fdd241'\n",
    "\n",
    "TEXT = \"欢迎使用百度语音合成。\"\n",
    "\n",
    "# 发音人选择, 基础音库：0为度小美，1为度小宇，3为度逍遥，4为度丫丫，\n",
    "# 精品音库：5为度小娇，103为度米朵，106为度博文，110为度小童，111为度小萌，默认为度小美 \n",
    "PER = 4\n",
    "# 语速，取值0-15，默认为5中语速\n",
    "SPD = 5\n",
    "# 音调，取值0-15，默认为5中语调\n",
    "PIT = 5\n",
    "# 音量，取值0-9，默认为5中音量\n",
    "VOL = 5\n",
    "# 下载的文件格式, 3：mp3(default) 4： pcm-16k 5： pcm-8k 6. wav\n",
    "AUE = 3\n",
    "\n",
    "FORMATS = {3: \"mp3\", 4: \"pcm\", 5: \"pcm\", 6: \"wav\"}\n",
    "FORMAT = FORMATS[AUE]\n",
    "\n",
    "CUID = \"123456PYTHON\"\n",
    "\n",
    "TTS_URL = 'http://tsn.baidu.com/text2audio'\n",
    "\n",
    "\n",
    "class DemoError(Exception):\n",
    "    pass\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN start \"\"\"\n",
    "\n",
    "TOKEN_URL = 'http://aip.baidubce.com/oauth/2.0/token'\n",
    "SCOPE = 'audio_tts_post'  # 有此scope表示有tts能力，没有请在网页里勾选\n",
    "\n",
    "\n",
    "def fetch_token():\n",
    "    print(\"fetch token begin\")\n",
    "    params = {'grant_type': 'client_credentials',\n",
    "              'client_id': API_KEY,\n",
    "              'client_secret': SECRET_KEY}\n",
    "    post_data = urlencode(params)\n",
    "    if (IS_PY3):\n",
    "        post_data = post_data.encode('utf-8')\n",
    "    req = Request(TOKEN_URL, post_data)\n",
    "    try:\n",
    "        f = urlopen(req, timeout=5)\n",
    "        result_str = f.read()\n",
    "    except URLError as err:\n",
    "        print('token http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "    if (IS_PY3):\n",
    "        result_str = result_str.decode()\n",
    "\n",
    "    print(result_str)\n",
    "    result = json.loads(result_str)\n",
    "    print(result)\n",
    "    if ('access_token' in result.keys() and 'scope' in result.keys()):\n",
    "        if not SCOPE in result['scope'].split(' '):\n",
    "            raise DemoError('scope is not correct')\n",
    "        print('SUCCESS WITH TOKEN: %s ; EXPIRES IN SECONDS: %s' % (result['access_token'], result['expires_in']))\n",
    "        return result['access_token']\n",
    "    else:\n",
    "        raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN end \"\"\"\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    token = fetch_token()\n",
    "    tex = quote_plus(TEXT)  # 此处TEXT需要两次urlencode\n",
    "    print(tex)\n",
    "    params = {'tok': token, 'tex': tex, 'per': PER, 'spd': SPD, 'pit': PIT, 'vol': VOL, 'aue': AUE, 'cuid': CUID,\n",
    "              'lan': 'zh', 'ctp': 1}  # lan ctp 固定参数\n",
    "\n",
    "    data = urlencode(params)\n",
    "    print('test on Web Browser' + TTS_URL + '?' + data)\n",
    "\n",
    "    req = Request(TTS_URL, data.encode('utf-8'))\n",
    "    has_error = False\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "\n",
    "        headers = dict((name.lower(), value) for name, value in f.headers.items())\n",
    "\n",
    "        has_error = ('content-type' not in headers.keys() or headers['content-type'].find('audio/') < 0)\n",
    "    except  URLError as err:\n",
    "        print('asr http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "        has_error = True\n",
    "\n",
    "    save_file = \"error.txt\" if has_error else 'result.' + FORMAT\n",
    "    with open(save_file, 'wb') as of:\n",
    "        of.write(result_str)\n",
    "\n",
    "    if has_error:\n",
    "        if (IS_PY3):\n",
    "            result_str = str(result_str, 'utf-8')\n",
    "        print(\"tts api  error:\" + result_str)\n",
    "\n",
    "    print(\"result saved as :\" + save_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "883a7798",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
